package com.quarters.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quarters.bean.User;
import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.ztree.ZtreeBean;
import com.quarters.mapper.UserMapper;
import com.quarters.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 个人信息 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    /**
     * @param wxMpUser
     * @return 微信登陆
     */
    @Override
    public User login(WxMpUser wxMpUser) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid", wxMpUser.getOpenId());
        User user = this.getOne(queryWrapper);
        //第一次而登陆  保存
        if (user == null) {
            user = new User();
            user.setCity(wxMpUser.getCity());
            user.setCountry(wxMpUser.getCountry());
            user.setProvince(wxMpUser.getProvince());
            user.setSex(wxMpUser.getSexDesc());
            user.setImge(wxMpUser.getHeadImgUrl());
            user.setNickName(wxMpUser.getNickname().getBytes());
            user.setOpenid(wxMpUser.getOpenId());
            user.setCreateTime(System.currentTimeMillis());
            this.save(user);
            //否则 更新性别 城市 头像
        } else {
            user.setCity(wxMpUser.getCity());
            user.setCountry(wxMpUser.getCountry());
            user.setProvince(wxMpUser.getProvince());
            user.setSex(wxMpUser.getSexDesc());
            user.setNickName(wxMpUser.getNickname().getBytes());
            this.updateById(user);
        }
        return user;
    }

    /**
     * @param userId
     * @return 解绑
     */
    @Override
    public Integer untying(Long userId) {
        return baseMapper.untying(userId);
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @Override
    public ResultPage<User> selectMyPage(VoPage voPage,Long id,Integer status) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("id", id);
        paramMap.put("status", status);
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param voPage
     * @return 是管理员查询所有的用户
     */
    @Override
    public ResultPage<User> selectMyPageAll(VoPage voPage,Integer status) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("status", status);
        List<User> list = baseMapper.selectMyPageAll(paramMap);
        Integer count = baseMapper.countMyPageAll(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }


    /**
     * @param userId
     * @return 发送文章查询当前用户下小区的用户
     */
    @Override
    public List<ZtreeBean> articleUser(Long userId) {
        return baseMapper.articleUser(userId);
    }
    /**
     * @param
     * @return 如果是超级管理员查询所有用户
     */
    @Override
    public List<ZtreeBean> articleUserAll( ) {
        return baseMapper.articleUserAll( );
    }

    /**
     * @param userId
     * @param quartersId
     * @param detailedFloor
     * @return 修改用户的小区楼层
     */
    @Override
    public Integer updateByIdUser(Long userId, Long quartersId, String detailedFloor,Long time) {
        return baseMapper.updateByIdUser(userId, quartersId, detailedFloor,time);
    }

    /**
     * @param userId
     * @return 根据当前用户查询当前用户所管理的小区的所有用户
     */
    @Override
    public List<User> selectUser(Long userId) {
        return baseMapper.selectUser(userId);
    }
    /**
     * @param
     * @return 如果是管理员就查询所有用户
     */
    @Override
    public List<User> selectUserAll() {
        return baseMapper.selectUserAll();
    }

    /**
     * @param orderId
     * @return 查询支付订单的管理员
     */
    @Override
    public User selectPayOpenId(Long orderId) {
        return baseMapper.selectPayOpenId(orderId);
    }

    /**
     * @param userId
     * @return 当前用户是否绑定小区
     */
    @Override
    public User binding(Long userId) {
        return baseMapper.binding(userId);
    }


}
